package oaSystem.biz.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import oaSystem.biz.AnnouncementBiz;
import oaSystem.entity.Announcement;
import oaSystem.mapper.AnnouncementMapper;

@Transactional//事务
@Service//业务
public class AnnouncementBizImpl implements AnnouncementBiz {
	
	@Autowired
	private AnnouncementMapper announcementMapper;
	
	@Override//存进草搞箱
	public void addAnnouncement(Announcement announcement) {
		//此时  参数的  announcement  创建时间 和 最后修改时间是一至的    审批时间为null  状态：草拟 0
		Date date = new Date();
		announcement.setCreateTime(date);
		announcement.setLastEditTime(date);
		announcement.setApproveTime(null);
		announcementMapper.addAnnouncement(announcement);//mapper添加公告
	}
	
	@Override//提交公告
	public void submitAnnouncement(Announcement announcement) {
	//事务start
		//此时  这个announcement  创建时间 和 最后修改时间是一至的    审批时间为null  状态：草拟 0
		announcement.setStatus(1);//将状态：待审批 1
		announcementMapper.updateAnnouncement(announcement);//mapper修改公告
		/*	并且   向这份公告的审批人 发送 邮件--》
		 	int 审批人Id = announcement.getApprover().getId();
			邮件biz还没有写  先顶着位置	*/
	//事务结束end
	}

	@Override//审批公告
	public void approveAnnouncement(int announcementId,int status) {
	//事务start
		Announcement announcement = announcementMapper.fetchAnnouncementById(announcementId);//查出单个公告  状态:待审批 1
		announcement.setApproveTime(new Date());//填入审批时间
		announcement.setStatus(status);//状态：2-通过审批，3-审批驳回
		announcementMapper.updateAnnouncement(announcement);//修改公告
	//事务结束end
	}

	@Override//申请人查看自己的公告    只为mapper做简单的代理
	public List<Announcement> findAnnouncementByCreator(int creatorId,int status,String keyword,int pageNum,int pageSize) {
		List<Announcement> list = announcementMapper.findAnnouncementByCreator(creatorId, status,keyword,(pageNum-1)*pageSize,pageSize);
		return list!=null?list:new ArrayList<Announcement>();
	}

	@Override//查看所有    只为mapper做简单的代理
	public List<Announcement> findAllAnnouncement() {
		List<Announcement> list = announcementMapper.findAllAnnouncement();
		return list!=null?list:new ArrayList<Announcement>();
	}

	@Override//查看所属公告   这里只是把所有 where 部门条件id罢了      (请打开   参考：学生设计示例 - 在线办公系统 倒数第二页)
	public List<Announcement> findAnnouncementByDepartmentId(int departmentId) {//只为mapper做简单的代理、
		List<Announcement> list = announcementMapper.findAnnouncementByDepartmentId(departmentId);
		return list!=null?list:new ArrayList<Announcement>();
	}

	@Override
	public Announcement fetchAnnouncementById(int AnnouncementId) {
		return announcementMapper.fetchAnnouncementById(AnnouncementId);
	}

	@Override
	public Integer findCountByCreator(int creatorId, int status, String keyword) {
		return announcementMapper.findCountByCreator(creatorId, status, keyword);
	}

	@Override//更新草稿箱
	public void updateDrafts(Announcement announcement) {//
		announcement.setStatus(0);
		announcementMapper.updateAnnouncement(announcement);
	}

	@Override//经理待审批的
	public List<Announcement> findAnnouncementByPending(Integer departmentId,Integer status,int pageNum,int pageSize) {
		return announcementMapper.findAnnouncementByPending(departmentId,status,(pageNum-1)*pageSize,pageSize);
	}

	@Override
	public Integer findCountByPending(Integer departmentId, int status) {
		return announcementMapper.findCountByPending(departmentId,status);
	}



}
